package com.zhengtoudai.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.zhengtoudai.R;

/**
 * 当前进度带圆球的进度条
 */

public class MyProgressBar extends FrameLayout {
    private Context mContext;
    private ProgressBar mProgressBar;   //进度条
    private View circle;                //圆球
    private int progressBarHeight;    //进度条高度
    private int progressDrawable= R.drawable.progressbar_style; //进度条风格
    private int progress;
    public MyProgressBar(Context context) {
        this(context,null);
    }

    public MyProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        //默认5dp
        progressBarHeight=(int)(mContext.getResources().getDisplayMetrics().density*5+0.5f);
        TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.MyProgressBar,defStyleAttr,0);
        //布局里面设置的高度
        progressBarHeight=a.getDimensionPixelSize(R.styleable.MyProgressBar_progressbar_height,progressBarHeight);
        //布局里面设置进度
        progress=a.getInteger(R.styleable.MyProgressBar_progress,progress);
        initView();
    }
    private void initView(){
        if (getChildCount()>0){
            removeAllViews();
        }
        mProgressBar=new ProgressBar(mContext,null,android.R.attr.progressBarStyleHorizontal);
        LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,progressBarHeight);
        params.gravity= Gravity.CENTER_VERTICAL;
        mProgressBar.setLayoutParams(params);
        mProgressBar.setMax(100);
        mProgressBar.setProgressDrawable(ContextCompat.getDrawable(mContext,progressDrawable));
        addView(mProgressBar);

        circle=new View(mContext);
        //圆球的大小为进度条的3倍
        LayoutParams vParams=new LayoutParams(progressBarHeight*3,progressBarHeight*3);
        vParams.gravity=Gravity.CENTER_VERTICAL;
        circle.setLayoutParams(vParams);
        circle.setBackgroundResource(R.drawable.shape_circle_red);
        addView(circle);

        setProgress(progress);
    }

    /**
     * 设置圆点位置在当前进度条位置
     */
    private void setViewX(){
        mProgressBar.post(new Runnable() {
            @Override
            public void run() {
                float currentX=mProgressBar.getWidth()*mProgressBar.getProgress()/mProgressBar.getMax();
                if (currentX<=0){
                    currentX=0;
                }else if (currentX>=mProgressBar.getWidth()-progressBarHeight*3){
                    currentX=mProgressBar.getWidth()-progressBarHeight*3;
                }
                circle.setTranslationX(currentX);
            }
        });

    }

    public void setProgress(int progress){
        mProgressBar.setProgress(progress);
        setViewX();
    }

    /**
     *
     * @param height 进度条高度  单位dp
     */
    public void setProgressBarHeight(int height){
        //px转dp
        progressBarHeight=(int)(mContext.getResources().getDisplayMetrics().density*height+0.5f);
        initView();
    }
}
